<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="dataForm" size="mini" :model="listQuery" label-position="right" label-width="auto">
        <div class="lineBox">
          <el-form-item label="类型：" prop="type">
            <el-select v-model="listQuery.type">
              <el-option label="全部" value="0" />
              <el-option label="实体" value="1" />
              <el-option label="属性" value="2" />
            </el-select>
          </el-form-item>
        </div>
        <div class="lineBox">
          <el-form-item label="输入头实体：" prop="head_entity_name">
            <el-input v-model="listQuery.head_entity_name" size="mini" placeholder="" style="width: 180px;" />
          </el-form-item>
        </div>
        <div class="lineBox">
          <el-form-item label="输入头实体值：" prop="head_entity_value">
            <el-input v-model="listQuery.head_entity_value" size="mini" placeholder="" style="width: 180px;" />
          </el-form-item>
        </div>

        <div class="lineBox">
          <el-form-item label="输入头尾体值：" prop="tail_entity_value">
            <el-input v-model="listQuery.tail_entity_value" size="mini" placeholder="" style="width: 180px;" />
          </el-form-item>
        </div>

        <div class="lineBox">
          <el-form-item label="关系名称：" prop="rela_name">
            <el-input v-model="listQuery.rela_name" size="mini" placeholder="" style="width: 180px;" />
          </el-form-item>
        </div>

        <div class="lineBox">
          <el-form-item label="文章id" prop="article_id">
            <el-input v-model="listQuery.article_id" size="mini" placeholder="" style="width: 180px;" />
          </el-form-item>
        </div>

        <div class="lineBox">
          <el-button size="mini" type="primary" icon="el-icon-search" @click="handleFilter">
            查询
          </el-button>
        </div>
      </el-form>
    </div>

    <div class="myTable" style="min-height: 600px;">
      <el-table :key="tableKey" :data="list" fit size="mini" highlight-current-row style="width: 100%;">
        <el-table-column label="ID" prop="id" align="center" width="50" />
        <el-table-column label="批次号" prop="number" align="center" min-width="80" />
        <el-table-column label="文章id" prop="article_id" align="center" min-width="80" />
        <el-table-column label="类型" prop="type" align="center" min-width="80">
          <template slot-scope="{row,$index}">
            <span v-if="row.type==1">实体</span>
            <span v-if="row.type==2">属性</span>
          </template>
        </el-table-column>
        <el-table-column label="头实体" prop="head_entity_name" align="center" min-width="80" />
        <el-table-column label="头实体值" prop="head_entity_value" align="center" min-width="200">
          <template slot-scope="{row,$index}">
            <el-input v-model="row.head_entity_value" size="mini" />
          </template>
        </el-table-column>
        <el-table-column label="关系名称" prop="rela_name" align="center" min-width="80" />
        <el-table-column label="尾实体名称" prop="tail_entity_name" align="center" min-width="80" />
        <el-table-column label="尾实体值" prop="tail_entity_value" align="center" min-width="200">
          <template slot-scope="{row,$index}">
            <el-input v-model="row.tail_entity_value" size="mini" />
          </template>
        </el-table-column>
        <el-table-column label="属性名称" prop="attribute_name" align="center" min-width="100" />
        <el-table-column label="属性值" prop="attribute_value" align="center" min-width="80" />

        <el-table-column label="提取时间" prop="create_time" align="center" min-width="100" />
        <el-table-column fixed="right" label="操作" align="center" min-width="160" class-name="small-padding fixed-width">
          <template slot-scope="{row,$index}">
            <el-button type="primary" size="mini" @click="handleUpdate(row,$index)">
              保存
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(row,$index)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pagination size="mini" :page-sizes="[10]" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      layout="total, sizes, prev, pager, next, jumper" @pagination="getList" />
    <addNoticeDlg ref="addNoticeDlg" @getList="getList" />
  </div>
</template>

<script>
import { tripletApi } from '@/api/triplet'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {

  components: { Pagination },

  data() {
    return {

      formInline: {
        user: "",
        region: ""
      },
      value: '',

      tableKey: 0,
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        pageNumber: 1,
        page: 1,
        limit: 10,
        type: "0",
        head_entity_name: "",
        rela_name: "",
        tail_entity_value: "",
        article_id: "",



      },


    }
  },



  created() {

    this.getList()
  },
  methods: {

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    // 数据列表
    async getList() {
      this.listLoading = true
      const response = await tripletApi("getTripletList", this.listQuery)
      if (response.code == 200) {
        this.list = response.data.resultList
        this.total = response.totalCount
        // this.type = response.type
        //   // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      }
    },
    async handleUpdate(row) {

      const url = 'saveTripletById'
      var res = await tripletApi(url, row)
      if (res.code == 200) {
        // this.htmlContent = res.data.content.replace(/\n\n/g, '</p><p>').replace(/\n/g, '<br>');
        this.$notify({
          title: '成功',
          message: res.message,
          type: 'success',
          duration: 2000
        })
      }




    },
    handleCreate() {
      this.$refs.addNoticeDlg.showDialog()
    },

    // 删除指标
    async handleDelete(row, index) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const data = { 'id': row.id }

        const response = await tripletApi("delTripletById", data)
        if (response.code == 200) {
          this.$notify({
            title: '成功',
            message: response.message,
            type: 'success',
            duration: 2000
          })
          this.list.splice(index, 1)
        } else {
          this.$notify({
            title: '失败',
            message: response.message,
            type: 'error',
            duration: 2000
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 修改跳转
    async skip(row, index) {

      this.$router.push('/template/save?id=' + row.id)

    }
  }
}
</script>

<style lang="scss" scoped>
.demo-image__preview {
  float: left;
  margin-left: 5px;
}
.demonstration {
  font-size: 12px;
  color: #333;
  font-weight: normal;
}
</style>
